﻿using MvcSimplyCleverPart3.Models;
using SimplyCleverMiddleTier;
using System.Linq;
using System.Web.Mvc;

namespace MvcSimplyCleverPart3
{
    

    public class AddressFormFilterAttribute : ActionFilterAttribute
    {

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var metadataCountry = CountryProvider.GetCountries();

            ViewResultBase viewResult = filterContext.Result  as ViewResultBase;
            AddressViewModel address = filterContext.Controller.ViewData.Model as AddressViewModel;

            viewResult.ViewName = "~/Views/Contact/Address" + metadataCountry.First(mc => mc.Code.Equals(address.CountryCode)).AddressFormatInfo.FormatKey + ".cshtml";
        }
    }

}